//
//  SISSHouseAddTakeLookViewController.m
//  SISSAPP
//
//  Created by 优房网 on 2017/8/2.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSAddHouseCoopTakeLookViewController.h"
#import "SISSPassengerResourceCooperationViewController.h"
#import "SISSNetworkingManager+CoopProgress.h"
#import "SISSHouseCoopTakeLookRecordViewController.h"

@interface SISSAddHouseCoopTakeLookViewController ()

/* 客源模型 */
@property (strong, nonatomic) SISSPassengerResourceModel *passengerModel;

@end

@implementation SISSAddHouseCoopTakeLookViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)chooseViewChooseAnction {
    
    SISSPassengerResourceCooperationViewController *vc = [[SISSPassengerResourceCooperationViewController alloc] init];
    vc.isProgressEnter = YES;
    vc.title = @"客源";
    vc.completeBlock = ^(SISSPassengerResourceModel *passengerModel) {
        
        self.chooseView.rightStr = @"已选择";
        self.passengerModel = passengerModel;
    };
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)confirmButtonClickAction:(UIButton *)sender {
    
    [super confirmButtonClickAction:sender];
    
    if (!self.dateStr) {
        [SISSTools showSVTitle:@"请选择看房时间" andTime:1.0 andColor:YES];
        return;
    }
    if (self.dateStr.length > 0 && [PublicUtil compareOneDate:[NSDate date] withAnotherDate:[PublicUtil getNSDateFromSimpleDateString:self.dateStr]] == NSOrderedAscending) {
        [self alertWithMessage:@"看房时间不能大于当前时间"];
        return ;
    }
    
    if (!self.passengerModel) {
        [SISSTools showSVTitle:@"请选择客源信息" andTime:1.0 andColor:YES];
        return;
    }
    if (self.images.count == 0) {
        [SISSTools showSVTitle:@"请选择看房照片" andTime:1.0 andColor:YES];
        return;
    }
    
    NSString *adbpId = self.passengerModel.adbpId.length ? self.passengerModel.adbpId : @"-1";
    NSString *timeStr = [self.dateStr stringByReplacingOccurrencesOfString:@"-" withString:@"/"];
    NSDictionary *parameters = @{
                                 @"saId":[SISSUserAccount shared].saId,
                                 @"ahcbId":self.houseCoopProgressModel.ID,
                                 @"adbpId":adbpId,
                                 @"abdId":self.passengerModel.ID,
                                 @"takeLookDate":timeStr ,
                                 @"takeLookType":@(self.passengerModel.demandType)
                                 };
    [kNetworkManager insertHouseCooprationTakeLookWithParameters:parameters images:self.images success:^(id responseObject) {
        
        if ([responseObject[@"code"] integerValue] != 200) {
            
            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                [SISSTools showSVTitle:responseObject[@"message"] andTime:1.0 andColor:YES];
            }];
            return ;
        }
        
        if ([responseObject[@"code"] integerValue] == 200) { // 添加成功
            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                [SISSTools showSVTitle:@"添加成功，等待对方确认。" andTime:1.0 andColor:YES];
            }];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.navigationController popViewControllerAnimated:YES];
            });
        }
        
    } failed:^(NSError *error) {
        
        NSLog(@"%@",error);
    }];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
